#pragma once
//协议定制！
#include <iostream>
#include <string>

#define SEP " ";
#define SEP_LEN strlen(SEP); //不敢使用sizeof()
#define LINE_SEP "\r\n"
#define LINE_SEP_LEN strlen(LINE_SEP)

const std::string &enLength(const std::string& text)
{

}

const std::string &deLength(const std::string& package)
{

}

class Request
{
public:
    Request():x(0),y(0),op(0)
   {} 
    Request(int x_,int y_, char op_):x(x_),y(y_),op(op_)
    {}
    bool serialize(std::string* out) //序列化
    {
        *out = "";
        //结构化-> "x op y"
        std::string x_string = std::to_string(x);
        std::string y_string = std::to_string(y);

        *out = x_string;
        *out += SEP;
        *out += op;
        *out += SEP;
        *out += y_string;
        *out += LINE_SEP;
    }
    // "x op y"
    bool deserialize(const std::string& in) //反序列化
    {
       // "x op y"->结构化
        auto left = in.find(SEP);
        auto right = in.rfind(SEP);

        if(left == std::string::npos || right == std::string::npos) return false;
        if(left == right) return false;
        if(right - (left+SEP_LEN) != 1) return false;

        std::string x_string = in.substr(0,left);  //[start,end)
        std::string y_string = in.substr(left+SEP_LEN);  //substr[start,步长])

        if(x_string.empty()) return false;
        if(y_string.empty()) return false;

        x = std::stoi(x_string);
        y = std::stoi(y_string);
        op = in[left+SEP_LEN];

        return true;
    }
public:
    // x op y
    int x;
    int y;
    char op;
};


class Response
{
public:
    Response():exitcode(0),result(0)
    {}
    bool serialize(std::string* out) //序列化
    {}
    bool deserialize(const std::string& in) //反序列化
    {}
public:
    int exitcode; // 0： 计算成功  !0:计算失败
    int result;   //计算结果
};
